package com.wyhello.tube4.spring.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.wyhello.tube4.pojo.Macroscopic;
import com.wyhello.tube4.pojo.Position;
import com.wyhello.tube4.pojo.Reality;
import com.wyhello.tube4.pojo.Reside;
import com.wyhello.tube4.pojo.Tube;
import com.wyhello.tube4.spring.model.AnalysisDrawModel;
import com.wyhello.tube4.spring.model.MeterRecordsModel;
import com.wyhello.tube4.spring.model.ResideRecordsModel;
@Controller
public class StatisticsController {
	@Autowired
	private AnalysisDrawModel analysisDrawModel;
	@Autowired
	private MeterRecordsModel meterRecordsModel;
	@Autowired
	private ResideRecordsModel resideRecordsModel;
	/**
	 * 查询锅炉信息
	 * @return
	 */
	@RequestMapping("/statistics/queryRealitys")
	public ModelAndView queryRealitys(){
		List<Reside> boilers =  meterRecordsModel.queryResideByBoiler();
		ModelAndView model = new ModelAndView("main");
		model.addObject("boilers",boilers);
		return model;
	}
	/**
	 * 查询指定锅炉下的受热面
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/statistics/querySurface")
	public void querySurface(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Reside> surfaceSet = reside.getResides();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Reside surface : surfaceSet) {
			map.put(surface.getId(), surface.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	/**
	 * 查询指定受热面下的管排
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/statistics/queryRows")
	public void queryRows(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Reside> rows = reside.getResides();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Reside row : rows) {
			map.put(row.getId(), row.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	/**
	 * 查询指定管排下的管
	 * @param parentid
	 * @return
	 */
	@RequestMapping("/statistics/queryTube")
	public void queryRow(int parentid,HttpServletResponse response){
		Reside reside = resideRecordsModel.getResideById(parentid);
		Set<Tube> tubes = reside.getTubes();
		Map<Integer,String> map = new TreeMap<Integer,String>();
		for (Tube tube : tubes) {
			map.put(tube.getId(), tube.getName());
		}
		JSONObject json = JSONObject.fromObject(map);
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
		} catch (IOException e) {
			e.printStackTrace();
		}
		out.print(json);
	}
	@RequestMapping("/statistics/statisticsTime")
	public ModelAndView diameterXTime(int tubeId,String begin,String end){
		//查询管信息
		Tube tube = resideRecordsModel.getTubeById(tubeId);
		//查询管下所有位置
		Set<Position> position = tube.getPositions();
		ModelAndView model = new ModelAndView("main");
		List<Reality> realitys = null;
		List<Macroscopic> macroscopics = null;
		
		List<Reality> realitysis = new ArrayList<Reality>();
		List<Macroscopic> macroscopicsis = new ArrayList<Macroscopic>();
		//循环管下所有位置查询每个位置的实际测量信息
		for (Position position2 : position) {
				realitys = analysisDrawModel.queryRealityTime(position2.getId(), begin, end,"outerDiameterX");
				for (Reality reality : realitys) {
					realitysis.add(reality);
				}
				macroscopics = meterRecordsModel.queryMacroscopicTime(position2.getId(), begin, end);
				for (Macroscopic macroscopic : macroscopics) {
					macroscopicsis.add(macroscopic);
				}
		}
		//查询锅炉信息，用于级联菜单
		List<Reside> boilers =  meterRecordsModel.queryResideByBoiler();
		model.addObject("boilers",boilers);
		model.addObject("tube",tube);
		model.addObject("realitys", realitysis);
		model.addObject("macroscopics", macroscopicsis);
		return model;
	}
}
